package org.yaac.client.ui;

import java.util.List;

import javax.inject.Inject;

import org.yaac.client.activity.OverviewActivity;
import org.yaac.client.widget.SortableColumn;
import org.yaac.shared.cap.CapSummaryDTO.Capability;

import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;

/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class OverviewImpl extends Composite implements Overview {

	private static OverviewUiBinder uiBinder = GWT.create(OverviewUiBinder.class);
	
	@UiTemplate("Overview.ui.xml")
	interface OverviewUiBinder extends UiBinder<Widget, OverviewImpl> {
	}

	@UiField(provided=true)
	CellTable<Capability> capTable;
	
	/**
	 * 
	 */
	@SuppressWarnings("unused")
	private OverviewActivity listener;

	/**
	 * 
	 */
	@Inject
	public OverviewImpl() {
		// init cap table
		capTable = new CellTable<Capability>();
		capTable.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
		
		// package name 
		// TODO : i18n
		capTable.addColumn(new SortableColumn<Capability, String>(new TextCell()) {
			@Override
			public String getValue(Capability cap) {
				return cap.getPackageName();
			}
		}, "Capability");
		
		// name 
		// TODO : i18n
		capTable.addColumn(new SortableColumn<Capability, String>(new TextCell()) {
			@Override
			public String getValue(Capability cap) {
				return cap.getName();
			}
		}, SafeHtmlUtils.fromSafeConstant("<br/>"));
		
		// status
		// TODO : i18n
		capTable.addColumn(new SortableColumn<Capability, String>(new TextCell()) {
			@Override
			public String getValue(Capability cap) {
				return cap.getStatus().toString();
			}
		}, "Status");
		
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public void setPresenter(OverviewActivity listener) {
		this.listener = listener;	
	}

	@Override
	public void updateCapabilities(List<Capability> caps) {
		ListDataProvider<Capability> dataProvider = new ListDataProvider<Capability>(caps);
		dataProvider.addDataDisplay(capTable);
	}
}
